package com.mjlf.mjatis.mapper;

import com.mjlf.mjatis.config.Configuration;
import com.mjlf.mjatis.sql.SqlBound;
import com.mjlf.mjatis.sql.SqlParse;
import lombok.Data;
import lombok.extern.java.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.sql.SQLException;

/**
 * @ClassName MapperMethod
 * @Author mjlft
 * @Date 2020/5/31 15:59
 * @Version 1.0
 * @Description Mapper中对应的method
 */
@Data
@Log
public class MapperMethod {
    protected String id;
    protected String type;
    protected String sql;
    protected String resultType;
    protected String parameterType;
    protected int timeOut;
    protected boolean useCache;
    protected SqlBound sqlBound = null;
    protected Class resultTypeClass;


    //执行具体的sql执行
    public Object execute(Configuration configuration, Object[] args, Parameter[] parameters) throws SQLException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Object result = null;
        //进行sql解析
        this.sqlBound = SqlParse.parseSql(sql, args, parameters);
        switch (type){
            case "insert":
            case "update":
            case "delete":
                result = configuration.getExecutor().update(configuration, this);
                break;
            case "select":
                result = configuration.getExecutor().query(configuration, this, this.getResultTypeClass());
                break;
        }

        return result;
    }
}
